﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using SymmetricEncription.Controls.AesControls;
using Org.BouncyCastle.Crypto.Utilities;

namespace SymmetricEncription.Presenters.AesSteps
{
    public class InitialTransformStep: ProcessStepBase
    {
        private byte[] block = new byte[0];
        public byte[] Block
        {
            get { return block; }
        }

        private byte[] blockWithKey = new byte[0];
        public byte[] BlockWithKey
        {
            get { return blockWithKey; }
            set
            {
                if (blockWithKey != null)
                {
                    blockWithKey = value;
                    RaisePropertyChanged("BlockWithKey");
                }
            }
        }

        private uint[,] w = new uint[0, 0];
        public uint[,] W
        {
            get { return w; }
            set
            {
                if (w != value)
                {
                    w = value;
                    RaisePropertyChanged("W");
                    RaisePropertyChanged("PlainW");
                }
            }
        }

        public InitialTransformStep(ProcessStepBase previousStep, byte[] block) : base(previousStep)
        {
            this.block = block;
            this.Title = "Выделение блока данных";
        }

        private InitialTransform stepUIControl = new InitialTransform();
        public override UIElement StepUIControl
        {
            get { return this.stepUIControl; }
        }

        public byte[] PlainW
        {
            get
            {
                try
                {
                    byte[] result = new byte[this.w.GetLength(0) * this.w.GetLength(1) * 4];
                    for (int i = 0; i < this.w.GetLength(0); i++)
                    {
                        for (int j = 0; j < this.w.GetLength(1); j++)
                        {
                            Pack.UInt32_To_LE(this.w[i, j], result, (i * this.w.GetLength(1) * 4) + j * 4);
                        }
                    }
                    return result;
                }
                catch
                {
                    return null;
                }
            }
        }
    }
}
